

            


            
using System;
using System.Collections.Generic;
using System.Web;
namespace DatabaseComponent
{
     public partial class Folders
     {

            //Write any additional methods or properties here 
            //Don't modify any genrated classes


         Folders _objMostParent = null;

         public Folders objMostParent
         {
             get {

                 if (_objMostParent == null)
                 {
                     Folders obj = this;

                     while (obj.ParentID != null)
                     {
                         obj = obj.objParent;

                     }


                     _objMostParent = obj;


                 }
                 
                 
                 
                 
                 return _objMostParent; }
             
         }

         



         Folders _objParent = null;

         public Folders objParent
         {
             get {

                 if (_objParent == null)
                 {

                     if (ParentID == null)
                         return new Folders();

                     FoldersDB objFoldersDB = new FoldersDB(ConnectionStrings.LMS);


                     List<Folders> lstFolders = objFoldersDB.getFoldersByID(ParentID.Value);

                     if (lstFolders.Count == 0)
                         return new Folders();


                     _objParent = lstFolders[0];
                 }
                 
                 
                 
                 return _objParent; }
             
         }



         List<Folders> _lstChild = null;

         public List<Folders> lstChild
         {
             get {

                 if (_lstChild == null)
                 {

                     FoldersDB objFoldersDB = new FoldersDB(ConnectionStrings.LMS);

                     List<Folders> lstFolders = objFoldersDB.getFoldersByParentID(ID);

                     if (lstFolders.Count == 0)
                     {
                         return lstFolders;

                     }


                     _lstChild = lstFolders;


                 }
                 
                 
                 
                 
                 
                 return _lstChild; }
             
         }


         List<Courses> _lstCourses = null;

         public List<Courses> lstCourses
         {
             get {

                 if (_lstCourses == null)
                 {


                     CoursesDB objCoursesDB = new CoursesDB(ConnectionStrings.LMS);

                     List<Courses> lstTCourses = objCoursesDB.getCoursesByFolderID(ID);

                     if (lstTCourses.Count == 0)
                         return lstTCourses;

                     _lstCourses = lstTCourses;


                 }
                 
                 
                 
                 
                 
                 
                 
                 
                 
                 return _lstCourses; }
             
         }



         /// <summary>
         /// this variable will have all courses in this folder and all its sub folders
         /// </summary>
         List<Courses> _lstRecursiveCourses = null;

         public List<Courses> lstRecursiveCourses
         {
             get {

                 if (_lstRecursiveCourses == null)
                 {
                     List<Courses> lstCurrentCourses = new List<Courses> ();
                     _lstRecursiveCourses = getRecursiveCourses(this, lstCurrentCourses);
                 }
                 
                 
                 
                 
                 return _lstRecursiveCourses; }
            
         }




         /// <summary>
         /// this function will get all courses in that folder recursive
         /// </summary>
         /// <param name="folders">The folder to search in</param>
         /// <param name="lstCurrentCourses">the courses found until this instance of Method</param>
         /// <returns>a list of courses returned</returns>
         private List<Courses> getRecursiveCourses(Folders folders, List<Courses> lstCurrentCourses)
         {
             
             

             if (folders._FolderNameEN == "..")
                 return lstCurrentCourses;

             if (folders.lstCourses.Count != 0)
                 addCoursesToListWithoutRepeating(folders.lstCourses, lstCurrentCourses);


             if (folders.ID != -1)
             {

                 foreach (Folders fd in folders.lstChild)
                 {
                     getRecursiveCourses(fd, lstCurrentCourses);
                 }
             }
             else
             {
                 FoldersDB objFoldersDB = new FoldersDB (ConnectionStrings.LMS);

                 foreach (Folders fd in objFoldersDB.Select("", " where [ParentID] is NULL OR [ParentID] = '-1' "))
                 {
                     getRecursiveCourses(fd, lstCurrentCourses);
                 }
             }



             return lstCurrentCourses;
         }



         /// <summary>
         /// this function will add a list of courses to another one without repeating
         /// </summary>
         /// <param name="source">The source list which will be added to destination list</param>
         /// <param name="dest">the destination list to add source to</param>
         private void addCoursesToListWithoutRepeating(List<Courses> source, List<Courses> dest)
         {
             foreach (Courses obj in source)
             {
                 if (!dest.Contains(obj))
                     dest.Add(obj);
             }

         }






         private string _PathEN = "";

         public string PathEN
         {
             get {

                 string strRet = "";

                 Folders objFolders = this;
                 strRet = "/" + objFolders.FolderNameEN + strRet;
                 while (objFolders.ParentID != null)
                 {
                     objFolders = objFolders.objParent;

                     strRet = "/" + objFolders.FolderNameEN + strRet;

                 }

                 _PathEN = strRet;
                 
                 return _PathEN; }
             
         }


         private string _PathAR = "";

         public string PathAR
         {
             get
             {

                 string strRet = "";

                 Folders objFolders = this;
                 strRet = "/" + objFolders.FolderNameAR + strRet;
                 while (objFolders.ParentID != null)
                 {
                     objFolders = objFolders.objParent;

                     strRet = "/" + objFolders.FolderNameAR + strRet;

                 }

                 _PathAR = strRet;

                 return _PathAR;
             }

         }






         private FolderImages _objFolderImages = null;

         public FolderImages objFolderImages
         {
             get {


                 if (FolderNameEN == ".." && FolderNameAR == "..")
                 {
                     FolderImages objNewFolderImages = new FolderImages();

                     objNewFolderImages.FolderID = ID;

                     objNewFolderImages.FolderImage = "FolderUp.png";
                     return objNewFolderImages;

                 }

                 if (_objFolderImages == null)
                 {
                     FolderImagesDB objFolderImagesDB = new FolderImagesDB(ConnectionStrings.LMS);
                     List<FolderImages> lstFolderImages = objFolderImagesDB.getFolderImagesByFolderID(ID);

                     if (lstFolderImages.Count == 0)
                     {
                         FolderImages objNewFolderImages = new FolderImages();

                         objNewFolderImages.FolderID = ID;
                         
                         objNewFolderImages.FolderImage = "Folder.jpg";

                         return objNewFolderImages;
                     }

                     _objFolderImages = lstFolderImages[0];
                 
                 }
                 
                 
                 return _objFolderImages; }
            
         }







         /// <summary>
         /// this is the location for image binding
         /// </summary>
         public string FolderImageUrl
         {
             get
             {
                
                     return "getimage.ashx?Image=./FolderImages/" + objFolderImages.FolderImage + "&w=50&h=50&color=white";
                
             }
         }

         /// <summary>
         /// this is the location for image binding
         /// </summary>
         public string FolderImageUrlSmall
         {
             get
             {

                 return "getimage.ashx?Image=./FolderImages/" + objFolderImages.FolderImage + "&w=20&h=20&color=white";

             }
         }






         


     }
}
